﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Collections.Generic;
using DDotNet.ORM.DataFactory;
using DDotNet.ORM.DataFactory.Interface;
using DDotNet.ORM.ORMapping.Exception;
using GenericTests.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenericTests
{
    /// <summary>
    /// Summary description for Carga
    /// </summary>
    [TestClass]
    public class Carga
    {
        public Carga()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        public void TestMethodDDOtnET()
        {
            ICrudFactory myFac = new CRUDFactory();
            DateTime Start = DateTime.Now;
            try
            {
                using (myFac)
                {
                    for (int i = 0; i < 5000; i++)
                    {

                        User newUser = new User();
                        newUser.Sex = i % 2 == 0 ? SexEnumerator.Fem : SexEnumerator.Masc;
                        newUser.Login = "BLA" + i.ToString();
                        newUser.Password = "123456";
                        newUser.FullName = i.ToString();
                        myFac.Update(newUser);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            DateTime end = DateTime.Now;
            TimeSpan total = end.Subtract(Start);
        }

        public void TestMethod()
        {
            OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Guilherme\Projetos.Net\DDotNet\src\GenericTests\Data\Unit.mdb;User Id=admin;Password=;");
            conn.Open();
            OleDbTransaction transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted);
            
            DateTime Start = DateTime.Now;
            try
            {
                for (int i = 0; i < 5000; i++)
                {
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = conn;
                    command.Transaction = transaction;
                    command.CommandText = "INSERT INTO TB_User([Login], [Password], [FullName], [Sex]) VALUES (?, ?, ?, ?)";
                    command.Parameters.Add(new OleDbParameter("Login", "BLA" + i.ToString()));
                    command.Parameters.Add(new OleDbParameter("Password", "123456"));
                    command.Parameters.Add(new OleDbParameter("FullName", i.ToString()));
                    command.Parameters.Add(new OleDbParameter("Sex", i % 2 == 0 ? 1 : 0));
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                conn.Close();
            }
            catch (Exception e)
            {
                Console.Write(e);
                transaction.Rollback();
            }
            DateTime end = DateTime.Now;
            TimeSpan total = end.Subtract(Start);
        }
    }
}
